(function() {
  var BuilderView, FormBuilder, ViewWidgetView, WidgetCollection, WidgetModel,
    extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
    hasProp = {}.hasOwnProperty;

  WidgetModel = (function(superClass) {
    extend(WidgetModel, superClass);

    function WidgetModel() {
      return WidgetModel.__super__.constructor.apply(this, arguments);
    }

    WidgetModel.prototype.sync = function() {};

    WidgetModel.prototype.initialize = function() {};

    WidgetModel.prototype.valid = function() {
      return Formbuilder.widgets[this.get(FormBuilder.options.mappings.WIDGET_TYPE)] != null;
    };

    WidgetModel.prototype.silder = function() {
      switch (this.get(FormBuilder.options.mappings.WIDGET_TYPE)) {
        case 'slider':
        case 'question':
          return $('#' + this.cid).slider({});
      }
    };

    return WidgetModel;

  })(Backbone.DeepModel);

  WidgetCollection = (function(superClass) {
    extend(WidgetCollection, superClass);

    function WidgetCollection() {
      return WidgetCollection.__super__.constructor.apply(this, arguments);
    }

    WidgetCollection.prototype.initialize = function() {
      return this.on('add', this.copyCidToModel);
    };

    WidgetCollection.prototype.model = WidgetModel;

    WidgetCollection.prototype.copyCidToModel = function(model) {
      return model.attributes.cid = model.cid;
    };

    return WidgetCollection;

  })(Backbone.Collection);

  ViewWidgetView = (function(superClass) {
    extend(ViewWidgetView, superClass);

    function ViewWidgetView() {
      return ViewWidgetView.__super__.constructor.apply(this, arguments);
    }

    ViewWidgetView.prototype.initialize = function(options) {
      this.parentView = options.parentView;
      return this.listenTo(this.model, "destroy", this.remove);
    };

    ViewWidgetView.prototype.events = {
      'change .slider-value': 'sliderValue',
      'change .form-control': 'writerJson'
    };

    ViewWidgetView.prototype.render = function() {
      var widget_type;
      widget_type = this.model.get(FormBuilder.options.mappings.WIDGET_TYPE);
      this.$el.data('cid', this.model.cid).html(FormBuilder.widgets[widget_type].view({
        rf: this.model
      }));
      rivets.bind(this.$el, {
        model: this.model
      });
      return this;
    };

    ViewWidgetView.prototype.sliderValue = function() {
      return this.model.attributes.value = $('#hidden-' + this.model.cid).val();
    };

    ViewWidgetView.prototype.writerJson = function() {
      return console.log(this.model.toJSON());
    };

    return ViewWidgetView;

  })(Backbone.View);

  BuilderView = (function(superClass) {
    extend(BuilderView, superClass);

    function BuilderView() {
      return BuilderView.__super__.constructor.apply(this, arguments);
    }

    BuilderView.prototype.SUBVIEWS = [];

    BuilderView.prototype.events = {
      'click .js-save-form': 'saveForm'
    };

    BuilderView.prototype.initialize = function(options) {
      var selector;
      selector = options.selector, this.formBuilder = options.formBuilder, this.bootstrapData = options.bootstrapData;
      if (selector != null) {
        this.setElement($(selector));
      }
      this.collection = new WidgetCollection();
      this.collection.bind('reset', this.reset, this);
      this.render();
      return this.collection.reset(this.bootstrapData);
    };

    BuilderView.prototype.addOne = function(responseWidget, _, options) {
      var view;
      view = new ViewWidgetView({
        model: responseWidget,
        parentView: this
      });
      if (options.$replaceEl != null) {
        return options.$replaceEl.replaceWith(view.render().el);
      } else {
        return this.$responseWidget.append(view.render().el);
      }
    };

    BuilderView.prototype.addAll = function() {
      return this.collection.each(this.addOne, this);
    };

    BuilderView.prototype.addField = function(e) {
      var widget_type;
      widget_type = $(e.currentTarget).data('field_type');
      return this.createField(Formbuilder.helpers.defaultFieldAttrs(widget_type));
    };

    BuilderView.prototype.createField = function(attrs, options) {
      var rf;
      return rf = this.collection.create(attrs, options);
    };

    BuilderView.prototype.createAndShow = function(model) {};

    BuilderView.prototype.reset = function() {
      this.$responseWidget.html('');
      return this.addAll();
    };

    BuilderView.prototype.render = function() {
      var i, len, ref, subview;
      this.$responseWidget = this.$el;
      ref = this.SUBVIEWS;
      for (i = 0, len = ref.length; i < len; i++) {
        subview = ref[i];
        new subview({
          parentView: this
        }).render();
      }
      return this;
    };

    return BuilderView;

  })(Backbone.View);

  FormBuilder = (function() {
    FormBuilder.helpers = {
      defaultFieldAttrs: function(field_type) {
        var attrs, base;
        attrs = {};
        attrs[Formbuilder.options.mappings.WIDGET_TYPE] = field_type;
        return (typeof (base = Formbuilder.widgets[field_type]).defaultAttributes === "function" ? base.defaultAttributes(attrs) : void 0) || attrs;
      },
      simple_format: function(x) {
        return x != null ? x.replace(/\n/g, '<br />') : void 0;
      }
    };

    FormBuilder.options = {
      mappings: {
        WIDGET_TYPE: 'field_type'
      }
    };

    FormBuilder.widgets = {};

    FormBuilder.registerWidgets = function(name, opts) {
      var i, len, ref, x;
      ref = ['view'];
      for (i = 0, len = ref.length; i < len; i++) {
        x = ref[i];
        opts[x] = _.template(opts[x]);
      }
      opts.field_type = name;
      return FormBuilder.widgets[name] = opts;
    };

    function FormBuilder(opts) {
      var args;
      if (opts == null) {
        opts = {};
      }
      _.extend(this, Backbone.Event);
      args = _.extend(opts, {
        form: this
      });
      this.mainView = new BuilderView(args);
    }

    return FormBuilder;

  })();

  window.FormBuilder = FormBuilder;

  if (typeof module !== "undefined" && module !== null) {
    module.exports = FormBuilder;
  } else {
    window.FormBuilder = FormBuilder;
  }

}).call(this);

(function() {
  FormBuilder.registerWidgets('question', {
    view: "<div class='form-group'>\n<label >评分</label>\n<div >\n<input id='<%=rf.cid %>' field_type='question' type=\"text\" />\n<script type='application/javascript'>\n\n  $(\"#<%=rf.cid %>\").slider({\n      value:<%=rf.get(\"defaultvalue\")%>,\n      step: 1,\n      min: 0,\n      max: <%=rf.get(\"max\")%>,\n      ticks: [<%=rf.get(\"ticks\") %>],\n      ticks_labels: [<%=rf.get(\"ticks\") %>],\n      ticks_snap_bounds: 30\n  }).on('change', function(event) {\n    $('#hidden-<%=rf.cid %>').val(event.value.newValue).change();\n   });\n</script>\n<input id='hidden-<%=rf.cid %>' type='hidden' class='slider-value form-control'>\n </div>\n</div>\n<div class='form-group'>\n<div >\n<table class=\"table table-bordered\">\n<% _.each(rf.get(\"table\"), function(item,index){ %>\n  <tr >\n    <td style=\"width:50px\">\n        <span class=\"label label-danger\"><%=item.score%></span>\n    </td>\n    <td><%=item.content%></td>\n  </tr>\n<% });%>\n</table>\n</div>\n</div>\n<div class=\"form-group\">\n<label for=\"\">评论</label>\n<textarea class=\"form-control\" rows=\"3\" rv-value=\"model:comment\"></textarea>\n</div>"
  });

}).call(this);

(function() {
  FormBuilder.registerWidgets('text', {
    view: "<div class='form-group'>\n<input type='text'id='<%=rf.cid %>' field-type='text' class='form-control'\n rv-value='model:value' placeholder=''\n aria-describedby='basic-addon2'>\n </div>"
  });

}).call(this);
